package com.ruoyi.app.service.impl;

import com.ruoyi.app.dto.LocationPointDto;
import com.ruoyi.app.mapper.ParkMapper;
import com.ruoyi.app.service.IParkService;
import com.ruoyi.app.vo.ParkingInfoVo;
import com.ruoyi.domain.ParkingInfo;
import com.ruoyi.mapper.ParkingInfoMapper;
import com.ruoyi.utils.DistanceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * App端Service业务层处理
 * 
 * @author dongdong
 * @date 2025-09-11
 */
@Slf4j
@Service
public class ParkServiceImpl implements IParkService {

    @Resource
    private ParkingInfoMapper parkingInfoMapper;

    @Override
    public List<ParkingInfoVo> getParkList(LocationPointDto locationPointDto) {
        if (locationPointDto == null || !locationPointDto.isValid()) {
            throw new RuntimeException("无效的坐标点");
        }
        List<ParkingInfo> parkingInfoList = parkingInfoMapper.selectParkingInfoList(new ParkingInfo());
        List<ParkingInfoVo> list = ParkMapper.INSTANCE.toParkingInfoVoList(parkingInfoList);
        List<ParkingInfoVo> voList = list.stream().map(e->{
            e.setDistance(DistanceUtil.calculateDistance(locationPointDto.getLatitude(), locationPointDto.getLongitude()
                    , e.getLatitude(), e.getLongitude()));
            e.setDistanceCN(DistanceUtil.getFormattedDistance(locationPointDto.getLatitude(), locationPointDto.getLongitude()
                    , e.getLatitude(), e.getLongitude()));
            return e;
        }).sorted(Comparator.comparingDouble(ParkingInfoVo::getDistance)).collect(Collectors.toList());
        return voList;
    }

}
